{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from tqdm import tqdm\n",
    "import csv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_data_path = '../data/testData0626.csv'\n",
    "route_order_folder_path = '../data/route_order_data_several'\n",
    "complete_train_path = '../data/complete_train_several.csv'\n",
    "port_path = '../data/port.csv'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_test_route_set(path):\n",
    "    test_route_info = {}\n",
    "    test_route_set = ['CNSHK-CLVAP','CNYTN-ARENA','CNSHK-GRPIR','CNHKG-ARBUE',\n",
    "                        'CNNSA-GHTEM','CNNSA-NAWVB','HKHKG-FRFOS','HONGKONG-BU']\n",
    "    for route in test_route_set:\n",
    "        ports = route.split(\"-\")\n",
    "        start_port = ports[0]\n",
    "        dest_port = ports[-1]    \n",
    "        test_route_info[route] = {}\n",
    "        test_route_info[route]['start_port'] = start_port\n",
    "        test_route_info[route]['dest_port'] = dest_port\n",
    "    return test_route_set, test_route_info\n",
    "test_route_set, test_route_info = get_test_route_set(test_data_path)\n",
    "\n",
    "def get_port_info():\n",
    "    port_data = {}\n",
    "    test_port_set = set()\n",
    "    for route in test_route_set:\n",
    "        ports = route.split('-')\n",
    "        test_port_set = set.union(test_port_set, set(ports))\n",
    "    port_data_origin = pd.read_csv(port_path)\n",
    "    for item in port_data_origin.itertuples():\n",
    "        if getattr(item, 'TRANS_NODE_NAME') in test_port_set:\n",
    "            port_data[getattr(item, 'TRANS_NODE_NAME')] = {'LONGITUDE': getattr(item, 'LONGITUDE'),'LATITUDE': getattr(item, 'LATITUDE') }\n",
    "    return port_data\n",
    "port_data = get_port_info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "csv_file = {}\n",
    "csv_writer = {}\n",
    "for route in test_route_set:\n",
    "    route_writer_path = os.path.join(route_order_folder_path, \"{}.csv\".format(route))\n",
    "    csv_file[route] = open(route_writer_path,'w',encoding='utf-8',newline='')\n",
    "    csv_writer[route] = csv.writer(csv_file[route])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "73it [59:47, 49.15s/it]\n"
     ]
    }
   ],
   "source": [
    "complete_train_chunk = pd.read_csv(complete_train_path, chunksize = 2000000, header=None\n",
    "                                          , names=['loadingOrder', 'timestamp','longitude','latitude','speed','TRANSPORT_TRACE'])\n",
    "for chunk in tqdm(complete_train_chunk):\n",
    "    for row in chunk.itertuples(index=False):\n",
    "        order_route = getattr(row, 'TRANSPORT_TRACE')\n",
    "        for test_route, route_info in test_route_info.items():\n",
    "            if ((route_info['start_port'] in order_route) and (route_info['dest_port'] in order_route)):\n",
    "                row_info = row._asdict()\n",
    "                row_info.pop('TRANSPORT_TRACE')\n",
    "                csv_writer[test_route].writerow(row_info.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "for k,v in csv_file.items():\n",
    "    v.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.7.6 64-bit ('AI': conda)",
   "language": "python",
   "name": "python37664bitaiconda6859e03b37c34f0182c9bde8073269f7"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
